package com.burst.reptile.modular.zp.service.impl;

import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.zp.entity.ZpRec;
import com.burst.reptile.modular.zp.mapper.ZpRecMapper;
import com.burst.reptile.modular.zp.service.IZpRecService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 推荐列表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-16
 */
@Service
public class ZpRecServiceImpl extends ServiceImpl<ZpRecMapper, ZpRec> implements IZpRecService {

	@Override
	public List<ZpRec> listAllByUserId(Long loginUserId) {
		return this.list(new QueryWrapper<ZpRec>().eq("user_id", loginUserId).orderByAsc("rec_sort"));
	}

	@Override
	@Transactional
	public int add(Long[] ids) {
		int i = 0;
		if(ToolUtil.isNotEmpty(ids)) {
			for (Long long1 : ids) {
				ZpRec rec = new ZpRec();
				rec.setArticleId(long1);
				Integer selectCount = this.baseMapper.selectCount(new QueryWrapper<ZpRec>().eq("article_id", long1));
				if(selectCount == 0) {
					i += this.baseMapper.insert(rec);
				}
			}
		}
		return i;
	}

	@Override
	@Transactional
	public int del(Long[] ids) {
		return this.baseMapper.delete(new QueryWrapper<ZpRec>().in("article_id", Arrays.asList(ids)));
	}

}
